package com.symone.webcms.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.symone.webcms.controller.base.BaseController;
import com.symone.webcms.model.DataContent;
import com.symone.webcms.model.Module;
import com.symone.webcms.model.base.ResultMessage;
import com.symone.webcms.service.DataContentService;
import com.symone.webcms.service.ModuleService;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *   @description : 模型管理
 *   ---------------------------------
 * 	 @author Archer Chu
 *   @since 2019-01-09
 */
@RestController
@Api(value="/module", description="模型管理")
@RequestMapping("api/module")
public class ModuleController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(ModuleController.class);

    @Autowired
    public ModuleService moduleService;

    /**
     * @description : 获取模型分页列表
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-09
     */
    @RequiresPermissions("dev:module:index")
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public ResultMessage page(Integer pageSize, Integer pageNum) {
        Wrapper<Module> ew = new EntityWrapper<Module>();
        ew.where("deleted=0");

        pageSize = (pageSize == null) ? 10 : pageSize;
        pageNum = (pageNum == null) ? 1 : pageNum;
        //logger.debug("SQL:"+ew.originalSql());

        Page<Module> page = new Page<Module>(Integer.valueOf(pageNum),Integer.valueOf(pageSize));
        moduleService.findPage(page, ew);

        return ResultMessage.success("获取模型的分页列表",page);
    }

    /**
     * @description : 获取所有模型记录
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-09
     */
    @RequiresPermissions("dev:module:index")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResultMessage list() {
        return ResultMessage.success("获取所有模型信息",moduleService.findAll());
    }

    /**
     * @description : 通过id获取模型
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-09
     */
    @RequiresPermissions("dev:module:get")
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public ResultMessage get(Long id) {
        return ResultMessage.success("获取模型信息成功!",moduleService.selectById(id));
    }

    /**
     * @description : 添加模型
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-09
     */
    @RequiresPermissions("dev:module:add")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResultMessage add(Module module){
        module.setCreateUser(getSessionUser().getUserName());
        if(module.getType() == 1) {
            module.setListApi("");
        }else{
            module.setListApi("/cms/" + module.getCode() + "/index");
        }
        module.setGetApi("/cms/"+module.getCode()+"/get");
        moduleService.insert(module);
        return ResultMessage.success("添加模型成功!");
    }

    /**
     * @description : 通过id逻辑删除模型
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-09
     */
    @RequiresPermissions("dev:module:remove")
    @RequestMapping(value = "/removeFlag", method = RequestMethod.PUT)
    public ResultMessage removeFlag(Long id){
        if(moduleService.deleteFlagById(id)) {
            return ResultMessage.success("逻辑模型删除成功！");
        } else {
            return ResultMessage.fail("逻辑模型删除失败！");
        }
    }

    /**
     * @description : 通过id逻辑删除模型
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-09
     */
    @RequiresPermissions("dev:module:remove")
    @RequestMapping(value = "/remove", method = RequestMethod.DELETE)
    public ResultMessage remove(long id){
        if(moduleService.deleteById(id)) {
            return ResultMessage.success("物理删除模型成功！");
        } else {
            return ResultMessage.fail("物理删除模型失败！");
        }
    }

    /**
     * @description : 更新模型信息
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-09
     */
    @RequiresPermissions("dev:module:edit")
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public ResultMessage update(Module module) {
        module.setUpdateUser(getSessionUser().getUserName());
        if(module.getType() == 1) {
            module.setListApi("");
        }else{
            module.setListApi("/cms/" + module.getCode() + "/index");
        }
        module.setGetApi("/cms/"+module.getCode()+"/get");
        if(moduleService.updateById(module)) {
            return ResultMessage.success("模型信息更新成功!");
        }else{
            return ResultMessage.fail("模型信息更新失败!");
        }
    }

    /**
     * @description : 更新模型信息
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-09
     */
    @RequestMapping(value = "/status", method = RequestMethod.POST)
    public ResultMessage updateStatus(Module module) {
        if(moduleService.updateById(module)) {
            return ResultMessage.success("模型信息更新成功!");
        }else{
            return ResultMessage.fail("模型信息更新失败!");
        }
    }

    /**
     * @description : 更新模型信息
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-09
     */
    @RequestMapping(value = "/records", method = RequestMethod.GET)
    public ResultMessage getRecordCount(Long id) {
        int count = moduleService.getRecordCount(id);
        return ResultMessage.success("获取" + count + "条内容记录数!",count);
    }

    /**
    * @description : 清除模型记录
    * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-09
    */
    @RequiresPermissions("dev:module:clear")
    @RequestMapping(value = "/clear", method = RequestMethod.DELETE)
    public ResultMessage clear(){
        int num = moduleService.clear();
        if(num > 0) {
            return ResultMessage.success("已经清理了 "+ num +" 条记录！");
        } else {
            return ResultMessage.fail("没有模型记录被清除！");
        }
    }

}
